package com.ask.forMe.utils;

import ch.qos.logback.core.model.INamedModel;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class IdGenerator {
    private Map<String, String> userConservationMap = new ConcurrentHashMap<>();
    public String generateConversationId() {
        String prefix = "CS"; // 客服系统前缀
        String dateCode = DateTimeFormatter.ofPattern("yyMMdd").format(LocalDateTime.now());
        String uuid = UUID.randomUUID().toString().replace("-", "").substring(0, 10);

        return String.join("-", prefix, dateCode, uuid);
        // 例如: CS-231105-a7d8f9e3b2
    }

    public String getOrCreateConversationID(Integer userId) {
        String today = DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDateTime.now());

        // 创建复合键：用户ID + 日期
        String compositeKey = userId + "_" + today;

        return userConservationMap.computeIfAbsent(compositeKey, id -> {
            return generateConversationId();
        });
    }


}
